//
//  XDTodoListViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-28.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDTodoListViewController.h"
#import "Get_Handle_GetUnHandleList_Task.h"
#import "CommonListCell.h"
#import "HandleModel.h"
#import "HandleList_Yb.h"
#import "XDTodoDetailViewController.h"
#import "Handle_GetHandleList_YiBan_Task.h"
#import "XDTodoDetailYbViewController.h"

@interface XDTodoListViewController ()
{
    NSMutableArray *dataArray,*dataArray_yb;
    int pageindex;
    int pagesize;
    int pageindex_yb;
    int pagesize_yb;
    NSString *keyword;
    NSString *getListTaskGuid,*getYbListTaskGuid;
    int dbType;//0待办，1已办
    
    int ybFirstLoad;
}
@end

@implementation XDTodoListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    pageindex = 1;
//    pagesize = 20;
//    keyword = @"";
//    dataArray = [[NSMutableArray alloc]init];
//    [self getData];
//    self.tv.tableFooterView = [self getFooterViewButtonNormal];
    pageindex_yb = 1;
    pagesize_yb = 20;
    keyword = @"";
    dataArray_yb = [[NSMutableArray alloc]init];
    [tabBar setSelectedItem:tbiDb];
}

-(void)getData
{
    self.tv.tableFooterView = [self getFooterViewLoad];
    NSMutableDictionary *params = [self getPassDictionary];
    [params setObject:@"" forKey:@"HandleType"];
    [params setObject:keyword forKey:@"keyword"];
    [params setObject:[NSString stringWithFormat:@"%i",pageindex] forKey:@"CurrentPageIndex"];
    [params setObject:[NSString stringWithFormat:@"%i",pagesize] forKey:@"pagesize"];
    [self showMBPHUD];
    getListTaskGuid = [[[Get_Handle_GetUnHandleList_Task alloc]initWithContext:self andParams:params]startTask];
    pageindex ++;
}

-(UIView *)getFooterViewButtonNormal
{
    UIButton *lb = [[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]autorelease];
    [lb setTitle:@"加载更多..." forState:UIControlStateNormal];
    [lb setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    if (dbType==0) {
        [lb addTarget:self action:@selector(getData) forControlEvents:UIControlEventTouchUpInside];
    }
    else
    {
        [lb addTarget:self action:@selector(getYbData) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return lb;
}

-(UIView *)getFooterViewLoad
{
    UIActivityIndicatorView *pb = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [pb setFrame:CGRectMake(0, 0, 320, 50)];
    [pb startAnimating];
    return [pb autorelease];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    [self doSearch:searchBar];
}

- (void)doSearch:(UISearchBar *)searchBar{

    keyword = [searchBar text];
    if (dbType==0) {
        pageindex = 1;
        [dataArray removeAllObjects];
        [self getData];
    }
    else
    {
        pageindex_yb = 1;
        [dataArray_yb removeAllObjects];
        [self getYbData];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_searchBar release];
    [_tv release];
    [_tvYb release];
    [tabBar release];
    [tbiDb release];
    [tbiYb release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setSearchBar:nil];
    [self setTv:nil];
    [self setTvYb:nil];
    [tabBar release];
    tabBar = nil;
    [tbiDb release];
    tbiDb = nil;
    [tbiYb release];
    tbiYb = nil;
    [super viewDidUnload];
}

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    if ([getListTaskGuid isEqualToString:threadGuid]) {
        NSMutableArray *temp = [[Get_Handle_GetUnHandleList_Task getListArray:[NSString stringWithFormat:@"%@",obj]]retain];
        for (int i=0; i<temp.count; i++) {
            [dataArray addObject:[temp objectAtIndex:i]];
        }
        self.tv.tableFooterView = [self getFooterViewButtonNormal];
        if (temp.count<pagesize) {
            self.tv.tableFooterView = nil;
        }
        [temp release];
        [self.tv reloadData];
    }
    else if([getYbListTaskGuid isEqualToString:threadGuid])
    {
        NSString *bs = (NSString *)obj;
        
        NSMutableArray *temp = [[Handle_GetHandleList_YiBan_Task getListArray:bs ]retain];
        for (int i=0; i<temp.count; i++) {
            [dataArray_yb addObject:[temp objectAtIndex:i]];
        }
        self.tvYb.tableFooterView = [self getFooterViewButtonNormal];
        if (temp.count<pagesize_yb) {
            self.tvYb.tableFooterView = nil;
        }
        [temp release];
        [self.tvYb reloadData];
        ybFirstLoad = 1;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *WebinfolistItem = @"WebinfolistItem";
    CommonListCell *cell = (CommonListCell *)[tableView dequeueReusableCellWithIdentifier:WebinfolistItem];
    if (cell==nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CommonListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    if (dbType==0) {
        HandleModel *m = [dataArray objectAtIndex:indexPath.row];
        cell.lbTitle.text = m.Title;
        
        cell.lbPerson.text = m.FromDispName;
        cell.lbDate.text = m.GenerateDate;
        cell.ivStatus.hidden = YES;
    }
    else
    {
        HandleList_Yb *m = [dataArray_yb objectAtIndex:indexPath.row];
        cell.lbTitle.text = m.Title;
        
        cell.lbPerson.text = m.FromDispName;
        cell.lbDate.text = m.GenerateDate;
        cell.ivStatus.hidden = YES;
    }
    cell.lbTitle.frame = CGRectMake(7, cell.lbTitle.frame.origin.y, 307, cell.lbTitle.frame.size.height);
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dbType==0) {
        return dataArray.count;
    }
    else
    {
        return dataArray_yb.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 71;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dbType==0) {
        HandleModel *m = [dataArray objectAtIndex:indexPath.row];
        
        if ([m.CanHanle isEqualToString:@"0"]) {
            [UITool toast:@"该待办请在PC上处理！"];
            return;
        }
        
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:m.FileType forKey:@"FileType"];
        [params setObject:m.MessageItemGuid forKey:@"MessageItemGuid"];
        [params setObject:self forKey:@"listContext"];
        XDTodoDetailViewController *vc = [[XDTodoDetailViewController alloc]initWithParams:params];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    else
    {
        return;
        HandleList_Yb *m = [dataArray_yb objectAtIndex:indexPath.row];
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:m.FileType forKey:@"FileType"];
        [params setObject:m.MessageItemGuid forKey:@"MessageItemGuid"];
        XDTodoDetailYbViewController *vc = [[XDTodoDetailYbViewController alloc]initWithParams:params];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma tabbar

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item==tbiDb) {
        dbType = 0;
        self.tv.hidden = NO;
        self.tvYb.hidden = YES;
        self.title = @"待办事宜";
    }
    else
    {
        dbType = 1;
        self.tv.hidden = YES;
        self.tvYb.hidden = NO;
        self.title = @"已办事宜";
        
        if (ybFirstLoad==0) {
            [self getYbData];
        }
    }
}

-(void)getYbData
{
    self.tvYb.tableFooterView = [self getFooterViewLoad];
    NSMutableDictionary *params = [self getPassDictionary];
    [params setObject:keyword forKey:@"KeyWord"];
    [params setObject:@"" forKey:@"HandleType"];
    [params setObject:[NSString stringWithFormat:@"%i",pageindex_yb] forKey:@"CurrentPageIndex"];
    [params setObject:[NSString stringWithFormat:@"%i",pagesize_yb] forKey:@"pagesize"];
    [self showMBPHUD];
    getYbListTaskGuid = [[[Handle_GetHandleList_YiBan_Task alloc]initWithContext:self andParams:params]startTask];
    pageindex_yb++;
}

-(void)viewDidAppear:(BOOL)animated
{
    pageindex = 1;
    pagesize = 20;
    keyword = @"";
    dataArray = [[NSMutableArray alloc]init];
    [self getData];
}

@end
